﻿<#
.SYNOPSIS
Redis++ 库构建脚本 (PowerShell 版本) - 完全保持原批处理逻辑
#>

# 设置错误处理
$ErrorActionPreference = "Stop"

# 1. 解压 Redis++ 源码 (如果不存在)
if (-not (Test-Path "redis-plus-plus-master")) {
    Write-Host "正在解压 redis-plus-plus-master.zip..."
    Expand-Archive -Path "redis-plus-plus-master.zip" -DestinationPath "."
}

# 进入源码目录
Set-Location "redis-plus-plus-master"

# 2. 创建输出目录结构
if (-not (Test-Path "..\lib")) { New-Item -ItemType Directory -Path "..\lib" | Out-Null }
if (-not (Test-Path "..\lib\x64")) { New-Item -ItemType Directory -Path "..\lib\x64" | Out-Null }
if (-not (Test-Path "..\lib\Win32")) { New-Item -ItemType Directory -Path "..\lib\Win32" | Out-Null }
if (-not (Test-Path "..\lib\x64\Debug")) { New-Item -ItemType Directory -Path "..\lib\x64\Debug" | Out-Null }
if (-not (Test-Path "..\lib\x64\Release")) { New-Item -ItemType Directory -Path "..\lib\x64\Release" | Out-Null }
if (-not (Test-Path "..\lib\Win32\Debug")) { New-Item -ItemType Directory -Path "..\lib\Win32\Debug" | Out-Null }
if (-not (Test-Path "..\lib\Win32\Release")) { New-Item -ItemType Directory -Path "..\lib\Win32\Release" | Out-Null }
if (-not (Test-Path "..\include")) { New-Item -ItemType Directory -Path "..\include" | Out-Null }

# 3. x64 Debug 构建
Write-Host "构建 x64 Debug 版本..."
New-Item -ItemType Directory -Path "build64d" -ErrorAction SilentlyContinue | Out-Null
Set-Location "build64d"
$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Debug -DREDIS_PLUS_PLUS_BUILD_TEST=OFF -DREDIS_PLUS_PLUS_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Debug
& cmake --install . --config Debug
Copy-Item -Path ".\dist\lib\*.lib" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\dist\bin\*.dll" -Destination "..\..\lib\x64\Debug" -Force
robocopy ".\dist\include\sw" "..\..\include\sw" /E /COPYALL /R:1 /W:1 /NFL /NDL | Out-Null
Set-Location ".."

# 4. x64 Release 构建
Write-Host "构建 x64 Release 版本..."
New-Item -ItemType Directory -Path "build64r" -ErrorAction SilentlyContinue | Out-Null
Set-Location "build64r"
$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Release -DREDIS_PLUS_PLUS_BUILD_TEST=OFF -DREDIS_PLUS_PLUS_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Release
& cmake --install . --config Release
Copy-Item -Path ".\dist\lib\*.lib" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\dist\bin\*.dll" -Destination "..\..\lib\x64\Release" -Force
Set-Location ".."

# 5. Win32 Debug 构建
Write-Host "构建 Win32 Debug 版本..."
New-Item -ItemType Directory -Path "build32d" -ErrorAction SilentlyContinue | Out-Null
Set-Location "build32d"
$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A Win32 -DCMAKE_BUILD_TYPE=Debug -DREDIS_PLUS_PLUS_BUILD_TEST=OFF -DREDIS_PLUS_PLUS_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Debug
& cmake --install . --config Debug
Copy-Item -Path ".\dist\lib\*.lib" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\dist\bin\*.dll" -Destination "..\..\lib\Win32\Debug" -Force
Set-Location ".."

# 6. Win32 Release 构建
Write-Host "构建 Win32 Release 版本..."
New-Item -ItemType Directory -Path "build32r" -ErrorAction SilentlyContinue | Out-Null
Set-Location "build32r"
$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A Win32 -DCMAKE_BUILD_TYPE=Release -DREDIS_PLUS_PLUS_BUILD_TEST=OFF -DREDIS_PLUS_PLUS_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Release
& cmake --install . --config Release
Copy-Item -Path ".\dist\lib\*.lib" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\dist\bin\*.dll" -Destination "..\..\lib\Win32\Release" -Force
Set-Location ".."

Write-Host "Redis++ 构建完成!"